﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using pay.DAL;
using pay.Entity;
using System.IO;

namespace pay.BLL
{
    public class ProductBLL
    {
        ProductDAL productDAL = new ProductDAL();

        public Product GetElementById(object id)
        {
            Product product= productDAL.GetElementById(id);
            //再从文件里面读
            //如果不为null
            //覆盖


            //var c = Newtonsoft.Json.JsonConvert.DeserializeObject<ProductImage[]>(StrImg);
            //if (c.Any(x => x.ProductId == product.Id))
            //{
            //    var temp= c.Single(x => x.ProductId == product.Id);
            //    if (temp.Price != null)
            //    {
            //        product.Nowprice = temp.Price;
            //    }
            //}
            return product;
        }

      
        private string StrImg
        {
            get
            {
                object obj = System.Web.HttpContext.Current.Cache["strImg"];
                if (obj != null)
                {
                    return obj.ToString();
                }
                using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/json/ProductImage.json")))
                {
                   string strImg = sr.ReadToEnd();
                   System.Web.HttpContext.Current.Cache.Add("strImg", strImg, null, DateTime.Now.AddMinutes(2),System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Low, null);
                   return strImg;
                }
            }
        }
    }
}
